package com.boboPlanet.user.controller;

import com.boboPlanet.comm.context.LoginUserContext;
import com.boboPlanet.comm.entity.po.LoginUserInfoPO;
import com.boboPlanet.comm.exception.BusinessException;
import com.boboPlanet.comm.result.ResponseResult;
import com.boboPlanet.user.annotation.LimitPass;
import com.boboPlanet.user.entity.dto.ArticleDTO;
import com.boboPlanet.user.entity.dto.ArticleMainDTO;
import com.boboPlanet.user.entity.po.ArticleDetailPO;
import com.boboPlanet.user.entity.po.CommentInfoPO;
import com.boboPlanet.user.entity.vo.ArticleMainVO;
import com.boboPlanet.user.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2023/2/22
 **/
@RestController
@RequestMapping("/planetArticles")
@Api(tags = "波仔星球文章帖子信息操作", value = "波仔星球文章帖子信息操作")
@Slf4j
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @PostMapping("/getLogoutArticleMainInfo")
    @ApiOperation("未登录获取文章信息-查询100条")
    @LimitPass
    public ResponseResult<List<ArticleMainVO>> getLogoutArticleMainInfo(@RequestBody ArticleMainDTO dto){
        LoginUserInfoPO userInfoPO = LoginUserContext.getLoginUserInfoPO();
        if (userInfoPO != null){
            LoginUserContext.clear();
        }
        return ResponseResult.isSuccess(articleService.getLogoutArticleMainInfo(dto,false));
    }

    @PostMapping("/getLoginArticleMainInfo")
    @ApiOperation("登录获取文章信息")
    public ResponseResult<List<ArticleMainVO>> getLoginArticleMainInfo(@RequestBody ArticleMainDTO dto){
        return ResponseResult.isSuccess(articleService.getLogoutArticleMainInfo(dto,true));
    }


    @PostMapping("/insertArticleInfo")
    @ApiOperation("添加文章信息")
    public ResponseResult<Boolean> insertArticleInfo(@RequestBody ArticleDTO dto){
        LoginUserInfoPO userInfoPO = LoginUserContext.getLoginUserInfoPO();
        if (!Optional.ofNullable(userInfoPO).isPresent()){
            throw BusinessException.build("用户未登录,请先登录...");
        }
        return ResponseResult.isSuccess(articleService.insertArticleInfo(dto));
    }

    @GetMapping("/getArtDetailById")
    @ApiOperation("通过id获取文章详细信息")
    @LimitPass
    public ResponseResult<ArticleDetailPO> getArtDetailById(@RequestParam Long artId){
        return ResponseResult.isSuccess(articleService.getArtDetailById(artId));
    }

    @GetMapping("/publishComment/{artId}")
    @ApiOperation("发表评论")
    public ResponseResult<Boolean> publishComment(@PathVariable Long artId,@RequestParam String content){
        log.info("评论参数====> {}",content);
        if (StringUtils.isBlank(content)){
            throw BusinessException.build("评论内容不能为空");
        }
        if (ObjectUtils.isEmpty(artId)){
            throw BusinessException.build("文章信息不能为空");
        }
        return ResponseResult.isSuccess(articleService.publishComment(artId,content));
    }

    @GetMapping("/getCommentList")
    @ApiOperation("获取文章评论列表")
    @LimitPass
    public ResponseResult<List<CommentInfoPO>> getCommentList(@RequestParam Long artId){
        if (ObjectUtils.isEmpty(artId)){
            throw BusinessException.build("文章信息不能为空");
        }
        return ResponseResult.isSuccess(articleService.getCommentList(artId));
    }

    @GetMapping("/collectArt/{artId}")
    @ApiOperation("收藏文章")
    public ResponseResult<Boolean> collectArt(@PathVariable Long artId){
        return ResponseResult.isSuccess(articleService.collectArt(artId));
    }

    @GetMapping("/cancelCollectArt/{artId}")
    @ApiOperation("取消收藏文章")
    public ResponseResult<Boolean> cancelCollectArt(@PathVariable Long artId){
        return ResponseResult.isSuccess(articleService.cancelCollectArt(artId));
    }

    @GetMapping("/judgeCurrentArtIsCollect/{artId}")
    @ApiOperation("判断当前文章是否被当前用户收藏")
//    @LimitPass
    public ResponseResult<Boolean> judgeCurrentArtIsCollect(@PathVariable Long artId){
        return ResponseResult.isSuccess(articleService.judgeCurrentArtIsCollect(artId));
    }
    



}
